package com.senno.service.impl;

import com.senno.entity.Employee;
import com.senno.entity.UserInfo;
import com.senno.service.EmployeeService;
import com.senno.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SpringSecurityUserService implements UserDetailsService {

//    @Autowired
//    private UserInfoService userInfoService;

    @Autowired
    private EmployeeService employeeService;

    @Override
    public UserDetails loadUserByUsername(String employeeName) throws UsernameNotFoundException {
        //根据员工编号查询员工信息
//        UserInfo userInfo = userInfoService.selectByUserName(username);
        Employee employee = employeeService.selectEmployeeByName(employeeName);

        //判断，如果员工对象信息为空则返回空
        if (employee ==null){
            return null;
        }
        //添加验证通过角色ROLE_ADMIN
        List<GrantedAuthority> list = new ArrayList<>();
        list.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        //封装成user对象 {noop表示密码为明文} 并返回
        User user = new User(employeeName,"{noop}"+employee.getEmployeePassword(),list);

        return user;
    }
}
